package com.example.redissession;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
public class MyController {
    private static final String SESS_USER_KEY = "SESS_USER_KEY";
    @RequestMapping("/login")
    public String login(HttpSession session){
        session.setAttribute(SESS_USER_KEY,"dafei");
        return "Session set success";
    }
    @RequestMapping("/user")
    public String getUser(HttpServletRequest request){
        String user = null;
        HttpSession session = request.getSession(false);
        if(session != null){
            Object userObj = session.getAttribute(SESS_USER_KEY);
            if(userObj != null){
                user = userObj.toString();
            }
        }
        return user;
    }
}
